package am.NotenProgramm.Frame;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;

import am.NotenProgramm.objects.Grade;
import am.NotenProgramm.objects.Subject;
import am.NotenProgramm.objects.myDate;


public class NewGradeFrame extends JFrame implements ActionListener,KeyListener{
	
	JLabel labelGradeName = new JLabel(" " + GradePro.rb.getString("tableName"));
	JLabel labelGrade = new JLabel(" " + GradePro.rb.getString("tableGrade"));
	JLabel labelGradeWeight = new JLabel(" " + GradePro.rb.getString("gradeWeight"));
	JLabel labelGradeIsOral = new JLabel(" " + GradePro.rb.getString("tableIsOral"));
	JLabel labelGradeDate = new JLabel(" " + GradePro.rb.getString("tableDate"));
	
	JTextField textFieldName = new JTextField("");
	public JFormattedTextField textFieldGrade = new JFormattedTextField(NumberFormat.getNumberInstance());
	JFormattedTextField textFieldWeight = new JFormattedTextField(NumberFormat.getNumberInstance());
	JCheckBox checkBoxIsOral = new JCheckBox();
	JPanel leftPanel = new JPanel();
	JPanel rightPanel = new JPanel();
	
	JPanel panelDateSelection = new JPanel();
	
	String[] months = {GradePro.rb.getString("january"),GradePro.rb.getString("february"),GradePro.rb.getString("march"),GradePro.rb.getString("april"),GradePro.rb.getString("may"),GradePro.rb.getString("june"),GradePro.rb.getString("july"),GradePro.rb.getString("august"),GradePro.rb.getString("september"),GradePro.rb.getString("october"),GradePro.rb.getString("november"),GradePro.rb.getString("december")};
	
	JSpinner spinnerDateDay = new JSpinner(new SpinnerNumberModel(1,1,31,1));
	JComboBox comBoxDateMonth = new JComboBox(months);
	JSpinner spinnerDateYear = new JSpinner(new SpinnerNumberModel(2013,1,9999,1));
	
	JButton buttonAccept = new JButton(GradePro.rb.getString("accept"));
	JButton buttonAbort = new JButton(GradePro.rb.getString("abort"));

	public NewGradeFrame(){
		setSize(370, 220);
		setTitle(GradePro.rb.getString("buttonNewGrade"));
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setAlwaysOnTop(true);
		setResizable(false);
		
		initUI();
		
		setLocationRelativeTo(null);
		setUndecorated(true);
		setVisible(true);
		textFieldName.requestFocusInWindow();
	}
	
	private void initUI() {
		buttonAbort.addActionListener(this); buttonAbort.setActionCommand("abort");
		buttonAccept.addActionListener(this); buttonAccept.setActionCommand("accept");
		textFieldGrade.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				try{
				if(Double.parseDouble(textFieldGrade.getText()) > 6){
					textFieldGrade.setValue(6);
				}
				if(Double.parseDouble(textFieldGrade.getText()) < 1){
					textFieldGrade.setValue(1);
				}}
				catch(Exception e1){
					textFieldGrade.setText("");
				}
				
			}
			
			
			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		textFieldName.addKeyListener(this);
		textFieldGrade.addKeyListener(this);
		textFieldWeight.addKeyListener(this);
		checkBoxIsOral.addKeyListener(this);
		panelDateSelection.addKeyListener(this);
		buttonAbort.addKeyListener(this);
		buttonAccept.addKeyListener(this);
		
		rightPanel.setLayout(new GridLayout(6,1));
		leftPanel.setLayout(new GridLayout(6,1));
		
		setLayout(null);
		
		add(rightPanel);
		rightPanel.setLocation(90,10);
		rightPanel.setSize(270,200);
		
		add(leftPanel);
		leftPanel.setLocation(10,10);
		leftPanel.setSize(80,200);		
		
		leftPanel.add(labelGradeName);
		rightPanel.add(textFieldName);
		
		leftPanel.add(labelGrade);
		rightPanel.add(textFieldGrade);
		
		leftPanel.add(labelGradeWeight);
		rightPanel.add(textFieldWeight);
		
		leftPanel.add(labelGradeIsOral);
		rightPanel.add(checkBoxIsOral);
		
		leftPanel.add(labelGradeDate);
		rightPanel.add(panelDateSelection); 
		
		panelDateSelection.setLayout(new GridLayout(1,3));
		
		panelDateSelection.add(spinnerDateDay); 		
		panelDateSelection.add(comBoxDateMonth);
		panelDateSelection.add(spinnerDateYear);
		
		leftPanel.add(buttonAbort);
		rightPanel.add(buttonAccept);
		
		spinnerDateDay.setValue(new myDate().getDay());
		comBoxDateMonth.setSelectedIndex(new myDate().getMonth()-1);
		spinnerDateYear.setValue(new myDate().getYear());
	}
	
	
	public void createNewGrade() {
		String nameStr = textFieldName.getText(); 
		String selectedSubject = GradePro.mainFrame.panelGrades.listSubjects.getSelectedValue().toString();
		if (nameStr != null && !nameStr.equals("")) {
			boolean isAlreadyHere = false;
			if (!isAlreadyHere) {
				for(int i = 0; i < GradePro.myProfile.getSubjects().length; i++){
					if(GradePro.myProfile.getSubjects()[i].getName().equals(selectedSubject)){
						
						Grade tmpGrade = new Grade(nameStr,Double.parseDouble(textFieldGrade.getText()), 
								Double.parseDouble(textFieldWeight.getText()), checkBoxIsOral.isSelected(), 
								new myDate(Integer.parseInt(spinnerDateDay.getValue().toString()),comBoxDateMonth.getSelectedIndex()+1,Integer.parseInt(spinnerDateYear.getValue().toString())));
						Subject tmpSubject = GradePro.myProfile.getSubjects()[i];
						tmpSubject.addGrade(tmpGrade);
						GradePro.myProfile.setSubject(tmpSubject, i);
						int tmpSelectedGradeValue = GradePro.mainFrame.panelGrades.listSubjects.getSelectedIndex();
						GradePro.mainFrame.panelGrades.refreshPanels();
						GradePro.mainFrame.panelStatistics.refresh();
						GradePro.mainFrame.panelGrades.listSubjects.setSelectedIndex(tmpSelectedGradeValue);
						break;
					}
				}
				GradePro.myStream.saveProfile(GradePro.myProfile);
			} else {
				System.out.println(nameStr
						+ GradePro.rb.getString("alreadyExists"));
				JOptionPane.showMessageDialog(null,
						nameStr + " " + GradePro.rb.getString("alreadyExists"));
			}
		}

	}
	
	void testForEasterEgg(){
		if(textFieldName.getText().equals("muh") || textFieldName.getText().equals("moo")){
			JOptionPane.showMessageDialog(null, "I love cows!");
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("abort")){
			GradePro.mainFrame.setEnabled(true);
			this.dispose();
		}
		if(e.getActionCommand().equals("accept")){
			accepted();
		}
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER){
			accepted();
		}
	}

	private void accepted() {
		if(checkTextFields()){
			createNewGrade();
			this.setAlwaysOnTop(false);
			testForEasterEgg();
			GradePro.mainFrame.setEnabled(true);
			this.dispose();
		}else{
			//doNothing
		}
		
		
	}

	private boolean checkTextFields() {
		boolean closeWindow = true;
		String selectedSubject = GradePro.mainFrame.panelGrades.listSubjects.getSelectedValue().toString();
		
		if(textFieldGrade.getText().equals("")){
			JOptionPane.showMessageDialog(this, GradePro.rb.getString("noGradeSet"));
			closeWindow = false;
		}else
		if(textFieldName.getText().equals("") && !textFieldGrade.getText().equals("")){
			
			for(int i = 0; i < GradePro.myProfile.getSubjects().length; i++){
				if(GradePro.myProfile.getSubjects()[i].getName().equals(selectedSubject)){
					textFieldName.setText(selectedSubject + " " + (GradePro.myProfile.getSubjects()[i].getGrades().length+1));
				}
			}
			if(textFieldWeight.getText().equals("")){
				textFieldWeight.setText("1");
			}
		} else if(textFieldName.getText().equals("") && textFieldGrade.getText().equals("") && textFieldWeight.getText().equals("")){
			JOptionPane.showMessageDialog(this, GradePro.rb.getString("noGradeSet"));
			closeWindow = false;
		}
		
		if(textFieldWeight.getText().equals("")){
			textFieldWeight.setText("1");
		}
		
		return closeWindow;
	}

}
